package cn.tsinghua.comsmacitccntsinghua.ui;

import android.content.Intent;
import android.graphics.Camera;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.wonderkiln.camerakit.CameraKitError;
import com.wonderkiln.camerakit.CameraKitEvent;
import com.wonderkiln.camerakit.CameraKitEventListener;
import com.wonderkiln.camerakit.CameraKitImage;
import com.wonderkiln.camerakit.CameraKitVideo;
import com.wonderkiln.camerakit.CameraView;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import cn.tsinghua.comsmacitccntsinghua.R;

/**
 * @Author yjl
 * @create 2022/1/7 8:56
 */
public class CameraActivity extends AppCompatActivity {


    //保存照片的路径
    private Uri mCameraUri;
    //保存照片的文件路径
    private String mCameraImagePath;

    Button takePhotoButton;
    CameraView cameraView;
    LinearLayout photoLinearLayout;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.take_photo_activity);
        //设置无Title
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //初始化surfaceView
        init();
        takePhotoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cameraView.captureImage();
            }
        });
    }


    private void init(){
        takePhotoButton = findViewById(R.id.btn_takephoto);
        photoLinearLayout = findViewById(R.id.photo_linearlayout);
        //设置拍照回传
        cameraView.addCameraKitListener(new CameraKitEventListener() {
            @Override
            public void onEvent(CameraKitEvent cameraKitEvent) {

            }

            @Override
            public void onError(CameraKitError cameraKitError) {

            }

            @Override
            public void onImage(CameraKitImage cameraKitImage) {
                ImageView imageView = new ImageView(CameraActivity.this);
                imageView.setImageBitmap(cameraKitImage.getBitmap());
                photoLinearLayout.addView(imageView);
            }

            @Override
            public void onVideo(CameraKitVideo cameraKitVideo) {

            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    //创建保存图片的文件
    @RequiresApi(api = Build.VERSION_CODES.KITKAT)
    private File createImageFile(){
        String imageName = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        if(!storageDir.exists()){
            storageDir.mkdir();
        }
        File tempFile = new File(storageDir,imageName);
        if(!Environment.MEDIA_MOUNTED.equals(Environment.getStorageState(tempFile))){
            return null;
        }
        return tempFile;
    }

}
